<style>
body {
	margin:0;
	padding:0;
}
div.gridbox_xp table.obj td {
	border-bottom:thin dashed;
	border-right:none;
}
</style>

<?
$categories = $this->enumIterator->toArray('category','category');
$categories['System'] = "System";
$categories['User'] = "User";
?>
<div id="enumLayout" style="position:relative;width:100%;height:450px;">
	<div id="enumLeftPane" style="width:100%;height:100%;margin:0px;">
		<div id="enumLeftToolbar"></div>
		<select id="enumCategorySelect" name="enumCategorySelect" onChange="enumUpdateListGrid()">
		<?foreach ($categories as $enumerationIdId=>$category):?>
		<option<?=($category=="User")?' selected="selected"':''?>><?=$category?></option>
		<?endforeach;?>
		</select>
		<div id="enumListGridContainer" style="position:relative;width:100%;height:84%;"></div>
	</div>

	<div id="enumRightPane" style="width:100%;height:100%;margin:0px;overflow:auto"></div>
</div>

<script>

function enumEditObject(enumerationId) {
	var winEditORM = dhxWins.createWindow('windowEditORMObjectId',60,10,400,400);
	winEditORM.setText('<?=__('Edit ORM Object')?>');
	winEditORM.attachURL('<?=$this->baseUrl?>/generic-edit.raw?enumerationId='+enumerationId,true);
	winEditORM.setModal(true);
	winEditORM.centerOnScreen();
}

function enumSingleUpdate(field,id,value,callback) {
	dojo.xhrPost({
		url: "<?=$this->baseUrl?>/enumerations-manager.raw/process-edit-single-item",
		handleAs: "json",
		content: {
			field: field,
			enumerationId: id,
			value: value,
		},
		load: function (data) {
			if (typeof callback == 'function') {
				callback(data);
			}
		},
		error: function (error) {
			alert(error);
			console.error ('Error: ', error);
		}
	});
}

function enumUpdateListGrid() {
	var enumCategorySelect = dojo.byId('enumCategorySelect');
	var enumCategory = enumCategorySelect.options[enumCategorySelect.selectedIndex].value;
	enumListGrid.clearAll();
	if (typeof enumItemsGrid != 'undefined') {
		enumItemsGrid.clearAll();
	}
	if (enumCategory == "select") {
		return;
	}
	enumListGrid.load('<?=$this->baseUrl?>/enumerations-manager.raw/list?category='+enumCategory,null,"json");
}

//Layouts
var enumLayout = new dhtmlXLayoutObject("enumLayout","2U");
enumLayout.setImagePath("<?=$this->baseUrl?>/img/");
var leftPaneWidth = enumLayout.cells("a").getWidth() / 2;
leftPaneWidth += leftPaneWidth / 2;
enumLayout.cells("a").setWidth(leftPaneWidth);
enumLayout.cells("a").setText('<?=__("Enumerations")?>');
enumLayout.cells("b").setText('<?=__("Enumeration Details")?>');
enumLayout.cells("a").hideHeader();
enumLayout.cells("b").hideHeader();
enumLayout.cells("a").attachObject("enumLeftPane");
enumLayout.cells("b").attachObject("enumRightPane");
enumLayout.setEffect('collapse',true);
enumLayout.setEffect("resize", true);

function enumHideNecessaryToolbars() {
	enumLeftToolbar.disableItem("tbNewObject");
	enumLeftToolbar.hideItem("tbNewObject");
	enumLeftToolbar.disableItem("tbEditObject");
	enumLeftToolbar.hideItem("tbEditObject");
}

// Left Pane Toolbar
var enumLeftToolbar = new dhtmlXToolbarObject("enumLeftToolbar","ClearSilver");
enumLeftToolbar.loadXML("<?=$this->baseUrl?>/enumerations-manager.raw/toolbar",enumOnToolbarLoaded);
function enumOnToolbarLoaded() {
	enumHideNecessaryToolbars();
}
enumLeftToolbar.setIconsPath("<?=$this->baseUrl?>/img/");
enumLeftToolbar.attachEvent("onClick",enumOnToolbarClicked);
function enumOnToolbarClicked(id) {
	switch(id) {
		case "tbNewEnumeration":
			enumerationEditWindow();
			break;
		default:
			alert('<?=__("Invalid event: ")?>'+id);
	}
}

// Left Pane Grid
var enumListGrid =  new dhtmlXGridObject("enumListGridContainer");
enumListGrid.setImagePath("<?=$this->baseUrl?>/img/");
enumListGrid.setHeader('<?=__('Enumerations')?>');
enumListGrid.setColTypes("ed");
enumListGrid.setSkin("xp");
enumListGrid.setInitWidths("*");
enumListGrid.init();
enumListGrid.load('<?=$this->baseUrl?>/enumerations-manager.raw/list',null,"json");

function enumReloadRightPane(id) {
	dojo.xhrGet({
		url: "<?=$this->baseUrl?>/enumerations-manager.raw/view?enumerationId=" + id,
		handleAs: "text",
		load: function (data) {
			dojo.setInnerHTML(dojo.byId("enumRightPane"),data);
		},
		error: function (error) {
			console.error ('Error: ', error);
		}
	});
}

enumListGrid.attachEvent("onRowSelect",enumOnRowSelected);
function enumOnRowSelected(rowId,cellIndex) {
	//enumHideNecessaryToolbars();
	enumReloadRightPane(rowId);
}

enumListGrid.attachEvent("onEditCell",enumOnEditCell);
function enumOnEditCell(stage,rowId,cellIndex,newValue,oldValue) {
	switch (stage) {
		case 2:
			if (newValue != oldValue) {
				enumSingleUpdate("name",rowId,newValue,enumUpdateListGrid);
			}
			break;
	}
	return true;
}

function enumerationEditWindow(enumerationId,parentId) {
	var additionalArgs = new Array();
	if (enumerationId) {
		additionalArgs.push("enumerationId="+enumerationId);
	}
	if (parentId) {
		additionalArgs.push("parentId="+parentId);
	}
	var winEditEnumeration = dhxWins.createWindow('windowEditEnumerationId',60,10,350,420);
	winEditEnumeration.setText('<?=__('Add/Edit Enumeration')?>');
	winEditEnumeration.attachURL('<?=$this->baseUrl?>/enumerations-manager.raw/edit?'+additionalArgs.join("&"),true);
	winEditEnumeration.setModal(true);
	winEditEnumeration.centerOnScreen();
}

enumUpdateListGrid();
</script>
